package org.zhaya.sdk.swamill;

import com.google.common.collect.Lists;
import io.logz.sawmill.parser.ConditionDefinition;
import io.logz.sawmill.parser.ConditionalExecutionStepDefinition;
import io.logz.sawmill.parser.ExecutionStepDefinition;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * SwitchConditionalExecutionStepDefinition
 */
public class SwitchConditionalExecutionStepDefinition extends ConditionalExecutionStepDefinition {

    private List<ConditionalExecutionStepDefinition> steps = Lists.newLinkedList();

    private int index = 0;

    private List<ExecutionStepDefinition> defaultStep = null;

    public SwitchConditionalExecutionStepDefinition(ConditionDefinition condition, List<ExecutionStepDefinition> onTrue, List<ExecutionStepDefinition> onFalse) {
        super(condition, onTrue, onFalse);
    }

    public void setCaseConditionList(List<ConditionalExecutionStepDefinition> steps) {
        this.steps = steps;
    }

    public void setDefaultStep(List<ExecutionStepDefinition> defaultStep) {
        this.defaultStep = defaultStep;
    }

    @Override
    public ConditionDefinition getConditionDefinition() {
        return this.steps.get(index).getConditionDefinition();
    }


    @Override
    public List<ExecutionStepDefinition> getOnTrue() {
        return this.steps.get(index).getOnTrue();
    }

    @Override
    public Optional<List<ExecutionStepDefinition>> getOnFalse() {

        int oldIndex = index;
        if (oldIndex >= steps.size() - 1) {
            return Optional.empty();
        }
        index++;

        ExecutionStepDefinition a = this;
        return Optional.of(new ArrayList<ExecutionStepDefinition>() {
            private static final long serialVersionUID = 1230119411255187157L;

            {
                add(a);
            }
        });
    }
}
